/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package cn.kcrx.autoweightapp

import android.app.Application
import android.content.Context
import androidx.multidex.MultiDex
import cn.kcrx.autoweightapp.net.OKHttpUpdateHttpService
import cn.kcrx.autoweightapp.utils.XToastUtils
import cn.kcrx.autoweightapp.utils.sdkinit.XBasicLibInit
import com.tencent.mmkv.MMKV
import com.xuexiang.xupdate.XUpdate
import com.xuexiang.xupdate.entity.UpdateError.ERROR
import com.xuexiang.xupdate.logs.ILogger
import com.xuexiang.xupdate.utils.UpdateUtils
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.HiltAndroidApp
import mu.KotlinLogging


/**
 * @author xuexiang
 * @since 2018/11/7 下午1:12
 */
@HiltAndroidApp
class MyApp : Application() {
    /***
     *@author miaobo
     * @since 2022/04.01 10:25
     *通用日志
     */
    private val logger= KotlinLogging.logger {}
    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        //解决4.x运行崩溃的问题
        MultiDex.install(this)
        logger.info { "程序已启动..." }

    }

    override fun onCreate() {
        super.onCreate()
        initLibs()
        initXUpdate()
        MMKV.initialize(applicationContext)
    }

    /**
     * 初始化基础库
     */
    private fun initLibs() {
        XBasicLibInit.init(this)
    }

    companion object {
        /**
         * @return 当前app是否是调试开发模式
         */
        val isDebug: Boolean
            get() = BuildConfig.DEBUG
    }

    private fun initXUpdate() {
        XUpdate.get().setILogger { priority, tag, message, t ->
            logger.error(
                "XUpdate message=$message",
                t
            )
        }
            .debug(true)
            .isWifiOnly(false) //默认设置只在wifi下检查版本更新
            .isGet(true) //默认设置使用get请求检查版本
            .isAutoMode(false) //默认设置非自动模式，可根据具体使用配置
            .param("versionCode", UpdateUtils.getVersionCode(this)) //设置默认公共请求参数
            .param("appKey", packageName)
            .setOnUpdateFailureListener { error ->
                //设置版本更新出错的监听
                logger.error { "版本更新出错:${error.detailMsg}" }
                if (error.code != ERROR.CHECK_NO_NEW_VERSION) {          //对不同错误进行处理
                    XToastUtils.error(error.detailMsg)
                } else {
                    XToastUtils.info(error.message!!)
                }
            }
            .supportSilentInstall(false) //设置是否支持静默安装，默认是true
            .setIUpdateHttpService(OKHttpUpdateHttpService()) //这个必须设置！实现网络请求功能。
            .init(this) //这个必须初始化
    }
}